﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class ProductPicEdit : PageBase
    {
        int _ProductId;
        int _PicId;
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["id"], out _ProductId);
            int.TryParse(Request.QueryString["picId"], out _PicId);

            InitData();
        }

        protected void InitData()
        {
            if (_ProductId > 0 && _PicId > 0)
            {
                BLL.ProductPics picBLL = new BLL.ProductPics();
                Model.ProductPics picModel = picBLL.GetModel(_PicId);
                Image1.ImageUrl = "~/uploadpic/" + picModel.Url;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            BLL.ProductPics picBLL = new BLL.ProductPics();
            Model.ProductPics picModel = picBLL.GetModel(_PicId);
            if (picModel == null)
            {
                picModel = new Model.ProductPics();
                picModel.CreateTime = DateTime.Now;
            }
            picModel.ProductId = _ProductId;
            picModel.IsMain = cbIsMain.Checked ? 1 : 0;
            picModel.IsActive = 1;
            picModel.UpdateTime = DateTime.Now;
            ImageUpload img = new ImageUpload();
            
            img.SavePath = "~/uploadpic/";
            //img.IsCreateImg = true;
            img.FormFile = File1;
            img.Upload();
            if (img.Error == 0)
            {
                picModel.Url = img.OutFileName;
                ImageClass.MakeThumbnail(Server.MapPath("~/uploadpic/" + picModel.Url),
                    Server.MapPath("~/uploadpic/" + picModel.Url.Split('.')[0] + "_m." + picModel.Url.Split('.')[1]), 360, 360, "Cut");
                ImageClass.MakeThumbnail(Server.MapPath("~/uploadpic/" + picModel.Url), 
                    Server.MapPath("~/uploadpic/" + picModel.Url.Split('.')[0] + "_s." + picModel.Url.Split('.')[1]), 120, 120, "Cut");
            }
            
            int picId = 0;
            string logContent = string.Empty;
            if (_PicId > 0)
            {
                picId = _PicId;
                picBLL.Update(picModel);
                logContent = ManagerName + " 编辑产品 " + _ProductId + " 图片，编号：" + picId;
            }
            else
            {
                 picId =picBLL.Add(picModel);
                 logContent = ManagerName + " 添加产品 " + _ProductId + " 图片，编号：" + picId;
            }

            BLL.Log logBLL = new BLL.Log();
            Model.Log logModel = new Model.Log();
            logModel.LogTitleId = 3;
            logModel.ObjectId = picId;
            logModel.UserId = int.Parse(UserId);
            logModel.ClientIP = IPHelper.GetClientIP();
            logModel.LogDesc = logContent;
            logModel.CreateTime = DateTime.Now;
            logBLL.Add(logModel);

            MessageBox.ShowAndRedirect(this.Page, "操作成功", "ProductPicList.aspx?act=add&id=" + _ProductId);
        }
    }
}